package com.hatricks.is.gld.work.archive;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

/**
 * @packageName: com.hatricks.is.gdnwork.archive
 * @className: ArchiveGenerate
 * @author: yanyuhui y568821795@outlook.com
 * @date: 2024-10-16 10:25
 * @description: 归档数据生成
 */
@SuppressWarnings("VulnerableCodeUsages")
@Slf4j
public class ArchiveGenerate {

    //
    public static final List<String> ERROR_MODEL = ListUtil.toList(
            "670cc12fd8cd190007826848",
            "66f95d49d23e65000705018b",
            "66f9016e918cfb0007623fbe",
            "66f4cc7efbb17700077b6758",
            "66ed215c918cfb00076125e5",
            "66ed15a8d23e65000703d83b",
            "66eceeedfbb17700077ab246",
            "66ece55fd23e65000703d400",
            "66ebd85bfbb17700077a9dd0",
            "66ebadebd23e65000703bcea",
            "66e274dbfbb17700077a16ce",
            "66e274bcfbb17700077a16cd",
            "66e11635b6d0b7000765b0f0",
            "66de6d62207381000882316b",
            "66d7c9e67c2c4f0007319b69",
            "66d12e92207381000868dc71",
            "66d1271d7359a50007daad4c",
            "66cf7306b6d0b7000764bfb5",
            "66ced3eb9460570007080a07",
            "66c6f749e6b7150008130965",
            "66c44040b8e8620007833bc2",
            "66c31099e6b7150007d6e586",
            "66bc5f61c8020d0007475bb7",
            "66bb06f5fd55240008c214b8",
            "66bb02cc41fbe500074e82e2",
            "66baf1ebfd55240008c212ab",
            "66bc827987c4a10007059603",
            "66bc8099b61506000757208e",
            "66b8871c44fb8c0007b6751e",
            "66ac9d7b651a420008cc6713",
            "66ab3e7291c6fa00073a5045",
            "66a9b12591c6fa00073a39c8",
            "66a1429d91c6fa000739b62c",
            "6695dd1ee894c100072e1929",
            "66cc2607d2786400078cfa34",
            "66c5522ae6b7150008f4633e",
            "668283c5b40b0f0007605d37",
            "66824c8085acf60007302277",
            "66bc813233ca360007cd571d",
            "66bc5d5e9aa7580008c85757",
            "66bb009541fbe500074e8291",
            "66bafef641fbe500074e8262",
            "66aae713b40b0f00079c46cb",
            "667b86a25600970007952dfe",
            "6678cfe57836f80008da0ee8",
            "667533c67836f8000866c972",
            "66c69328e6b7150007c77788",
            "66c2c0bf26f481000754d541",
            "66bec370532d3b0007b7f5de",
            "66bc89b4532d3b0007b7dd24",
            "66bc81de33ca360007cd5727",
            "66bc7fbbb615060007572081",
            "668b9f6235e1e1000788ee1d",
            "66bc6002b615060007571dc5",
            "66bc5e1fb615060007571d64",
            "66863f94b40b0f0009b69a2b",
            "66863c07b40b0f0009b699fd",
            "66bb05e3fd55240008c2149f",
            "66baffcb41fbe500074e827f",
            "6684a585da29130007a99751",
            "667d3b0907917c0007bebbc2",
            "66751fe07836f8000841238a",
            "667396cd85acf600072f7370",
            "666beb2acfecbb00074a7c4e",
            "66543762fc997300073278f3",
            "668b461db40b0f0008675e5d",
            "666ec0e185acf600072f3262",
            "666be9cdcfecbb00074a7c4b",
            "6683c95f35e1e1000788a26c",
            "664eef8879ccf10007cada1c",
            "664d98ca1207d30007b1909b",
            "667a35c45600970007951bbf",
            "66753f3b7836f80007723f02",
            "667516fd7836f8000841237d",
            "6674dbe97836f800073d8e8d",
            "6673cdcf85acf600072f7787",
            "66542551c9dfb90007f570dd",
            "6672c86607917c0007be230e",
            "66449703988fad000778f13e",
            "666c3a4807917c0007bdd44c",
            "666c3c5756009700079454ca",
            "6662b4adf045700007baf2be",
            "66567f692e6b3100071f006b",
            "664d9c1f1207d30007b190e7",
            "664d6c3c1207d30007b18ce7",
            "664d6e6144c18d000837b14b",
            "662e1647de58690007abaed2",
            "662deea4de58690007aba8da",
            "662b878992bcaa00074cfaf5",
            "6628b9635da392000700cdff",
            "66223e50892e4f00077cdad9",
            "662b868792bcaa00074cfaed",
            "662b5d7e5da3920008df0fec",
            "66166b721193d50008e7b378",
            "662b891192bcaa00074cfafd",
            "66166a831193d50008e7b36c",
            "66084793f08d230007af5008",
            "660516e428d22d0008d250a2",
            "65fa79769c3d5100086a07c6",
            "65fa85dd9c3d5100086a07d7",
            "65fa82669c3d5100086a07d2",
            "65f9519b9c3d5100084f7bdb",
            "661654b08f3b510007867122",
            "65f443c99cbf03000788a4c2",
            "65f433607dc24800071a1060",
            "66084151f08d230007af4fba",
            "6606b8b6feeb1d00076f4522",
            "66069c929cbf03000741eb10",
            "660533c67dc2480008a64dd8",
            "65d5a1e6e49f6a0007bc0b74",
            "65bcb912f56a76000755dcea",
            "65fa7e079c3d5100086a07c7",
            "65d59dabe49f6a0007bc0b73",
            "65ba633f8109a2000731dfe5",
            "65bc5261f56a76000755dbd0",
            "657809bd330e85000764713d",
            "657967555b8be700076a69b6",
            "656db365cecac00007297721",
            "656db03bcecac0000729771f",
            "662f7525988fad000777ab14",
            "662f72ff988fad000777aaeb",
            "662b8ebafd1b250007938397");

    @Data
    static
    class NormalItem {
        private String mongodbId;
        private String createTime;
        private String location;
        private String modelSize;
    }

    @Data
    static class ReturnData {
        private List<String> error;
        private List<NormalItem> normal;
        // getters and setters
    }

    @Data
    class Response {
        private boolean success;
        private ReturnData data;
        private String status;
        private String message;
        // getters and setters
    }

    @SuppressWarnings("LoggingSimilarMessage")
    public static void main(String[] args) {
        String archive = "https://dcost-sub-pre.glodon.com";
        String standardTime = "2024-10-14 20:00:00";

        HashSet<String> removeDuplicationIds = new HashSet<>(ERROR_MODEL);
        if (removeDuplicationIds.size() != ERROR_MODEL.size()) {
            log.info("当前有重复数据,重复数据的条数为:[{}]", ERROR_MODEL.size() - removeDuplicationIds.size());
            log.info("当前有效的数据为:[{}]", JSON.toJSONString(removeDuplicationIds));
        } else {
            log.info("当前有效的数据为:[{}]", JSON.toJSONString(ERROR_MODEL));
        }


        try (HttpResponse response = HttpUtil.createPost(StrUtil.format("{}/api/service/archive/bizData/sniffingodsmodeltime", archive))
                .header("Content-Type", "application/json")
                .form("standardTime", standardTime)
                .body(JSONUtil.toJsonStr(ERROR_MODEL))
                .execute()) {
            if (response.isOk()) {
                String resultStr = response.body();
                Response responseObj = JSONUtil.toBean(JSONUtil.parseObj(resultStr), Response.class);
                List<NormalItem> normalItems = responseObj.getData().getNormal();
                List<Pair<String, String>> result = new LinkedList<>();
                for (NormalItem normal : normalItems) {
                    DateTime createTime = DateUtil.parseDateTime(normal.getCreateTime());
                    DateTime endTime = DateUtil.date();
                    if (!DateUtil.isIn(createTime, DateUtil.parseDateTime(standardTime), endTime)) {
                        continue;
                    }
                    String formattedStr = StrUtil.format(
                            "db.project.updateOne(\n" +
                                    "      {  _id: { $in: [ObjectId(\"{}\") ] } },\n" +
                                    "      { $set: { location: '{}', uploadBizDataStatus: true } }\n" +
                                    ");", normal.getMongodbId(), normal.getLocation());
                    //System.out.println(formattedStr);
                    System.out.println(StrUtil.format("当前需要处理的工程Id为:[{}]", normal.getMongodbId()));
                    result.add(Pair.of(normal.getLocation() + "@@" + normal.getModelSize(), normal.getCreateTime()));
                }
                log.info("当前模型的更新时间数据为:[{}]", JSON.toJSONString(result));
            } else {
                System.out.println("Request failed with status code: " + response.getStatus());
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }

}
